<script setup lang="ts">
import { Lock, User } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import useUserStore from '@/store/modules/user.ts'
// import router from '@/router'
import { ElNotification } from 'element-plus'
import { getTime } from '@/utils/time.ts'
import { useRoute, useRouter } from 'vue-router'
const route = useRoute()
const router = useRouter()
const loginForm = reactive({ username: 'admin', password: '111111' })
const user = useUserStore()
const loading = ref(false)
const ruleFormRef = ref()
const login = async (formEl: any) => {
  loading.value = true
  if (!formEl) return
  await formEl.validate(async (valid: any, fields: any) => {
    if (valid) {
      try {
        await user.userLogin(loginForm)
        let toPath: any = route.query.paths
        await router.push({ path: toPath || '/' })
        ElNotification({
          title: `Hi,${getTime()}好！`,
          message: '欢迎回来',
          type: 'success',
          duration: 3000,
        })
        loading.value = false
      } catch (error) {
        ElNotification({
          message: (error as Error).message,
          type: 'warning',
          duration: 3000,
        })
        loading.value = false
      }
    } else {
      console.log('error submit!', fields)
    }
  })
}

const rules = {
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [
    { required: true, min: 6, max: 12, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 12, message: '请输入6到12位的密码', trigger: 'change' },
  ],
}
</script>

<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="form_container"
          :model="loginForm"
          :rules="rules"
          ref="ruleFormRef"
        >
          <h1>Hello</h1>
          <p>欢迎来的二木儿甄选平台</p>
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              :prefix-icon="User"
              placeholder="请输入用户名"
              clearable
            />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              :prefix-icon="Lock"
              placeholder="请输入密码"
              clearable
              show-password
            />
          </el-form-item>
          <el-form-item>
            <el-button
              :loading="loading"
              type="primary"
              class="form_btn"
              @click="login(ruleFormRef)"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;
  .form_container {
    position: relative;
    width: 80%;
    top: 30vh;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    padding: 60px;
    color: white;
    h1 {
      font-size: 42px;
    }
    p {
      margin: 20px 0;
      font-size: 24px;
    }
    .form_btn {
      width: 100%;
    }
  }
}
</style>
